package com.hzhk.happyrent.ui.activity.personal;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.CodeModel;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.view.ClearEditText;
import com.hzhk.happyrent.utils.MyPerference;
import com.tamic.novate.Throwable;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PwdChangeActivity extends AppCompatActivity {

    @BindView(R.id.activity_titlebar_left)
    ImageView activityTitlebarLeft;
    @BindView(R.id.activity_titlebar_center)
    TextView activityTitlebarCenter;
    @BindView(R.id.phone_code_send)
    TextView phoneCodeSend;
    @BindView(R.id.reg_vcode)
    ClearEditText regVcode;
    @BindView(R.id.btn_sendvcode)
    Button btnSendvcode;
    @BindView(R.id.act_login_layout)
    LinearLayout actLoginLayout;
    @BindView(R.id.btn_register)
    Button btnRegister;
    TimeCount time;

    public static final int REQUEST_CODE=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pwd_change);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        activityTitlebarCenter.setText(R.string.code_verify);
        String phone= MyPerference.getInstance().getUserPhone();
        phoneCodeSend.setText(phone.substring(0,3)+"****"+phone.substring(7,11));
        time = new TimeCount(59300, 100);
    }

    @OnClick({R.id.activity_titlebar_left, R.id.btn_sendvcode, R.id.btn_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_titlebar_left:
                finish();
                break;
            case R.id.btn_sendvcode:
                getVCode();
                break;
            case R.id.btn_register:
                judgeVCode();
                break;
        }
    }
    //发送验证码
    private void getVCode() {
        Map<String, Object> params = new HashMap<>();
        params.put("phone", MyPerference.getInstance().getUserPhone());
        HttpManager.getInstance().perform_post(API.SMS_SEND, params, new MyStringResponse(this) {
            @Override
            public void onNext(Object tag,String response) {
                CodeModel baseModel = new Gson().fromJson(response, CodeModel.class);
                if (0 == baseModel.getErrCode()) {
                    Toast.makeText(PwdChangeActivity.this, R.string.apply_success, Toast.LENGTH_SHORT).show();
                    time.start();
                } else {
                    Toast.makeText(PwdChangeActivity.this, baseModel.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Object tag, Throwable e) {
//                Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }
        });
    }
    //判断验证码
    private void judgeVCode() {
        if(TextUtils.isEmpty(regVcode.getText().toString())){
            Toast.makeText(PwdChangeActivity.this, R.string.tv_vcode_is_null, Toast.LENGTH_SHORT).show();
            return;
        } else if(regVcode.getText().toString().length()!=6){
            Toast.makeText(PwdChangeActivity.this, R.string.tv_isnot_vcode, Toast.LENGTH_SHORT).show();
            return;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("phone", MyPerference.getInstance().getUserPhone());
        params.put("code", regVcode.getText().toString());
        HttpManager.getInstance().perform_post(API.CHECK_SMS, params, new MyStringResponse(this) {
            @Override
            public void onNext(Object tag,String response) {
                CodeModel baseModel = new Gson().fromJson(response, CodeModel.class);
                if (0 == baseModel.getErrCode()) {
                    Intent intent=new Intent(PwdChangeActivity.this,PwdInputActivity.class);
                    intent.putExtra("code",regVcode.getText().toString());
                    startActivityForResult(intent,REQUEST_CODE);
                } else {
                    Toast.makeText(PwdChangeActivity.this, baseModel.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Object tag, Throwable e) {
//                Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }
        });
    }
    // 验证码倒计时
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            btnSendvcode.setText(R.string.get_again);
            btnSendvcode.setEnabled(true);
            btnSendvcode.setBackgroundDrawable(getResources().getDrawable(R.drawable.img_time_tick_w));
            btnSendvcode.setTextColor(getResources().getColor(R.color.black_60));
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            btnSendvcode.setBackgroundDrawable(getResources().getDrawable(R.drawable.img_time_tick_g));
            btnSendvcode.setTextColor(getResources().getColor(R.color.white_90));
            btnSendvcode.setEnabled(false);
            btnSendvcode.setText((millisUntilFinished + 800) / 1000 + "s");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(RESULT_OK==resultCode&&REQUEST_CODE==requestCode){
            finish();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        time.cancel();
    }
}
